﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace huaguwang
{
    /// <summary>
    /// 对用户的页面操作集合方法
    /// <para>
    /// 或者是获取当前的用户信息
    /// </para>
    /// </summary>
    public static class UserConst
    {
        /// <summary>
        /// 专门处理权限错误的方法
        /// </summary>
        /// <param name="e"></param>
        public static void Exp_ToolBarErr(this System.Web.UI.Page page, WebControls.WebUserToolBar.enumType e)
        {
            switch(e)
            {
                case huaguwang.WebControls.WebUserToolBar.enumType.Finish:
                    page.Exp_PageAlterReplace("运行系统错误", "/hgmanage/login.aspx");
                    break;
                case huaguwang.WebControls.WebUserToolBar.enumType.InitErr:
                    page.Exp_PageAlterReplace("工具条初始化失败", "/hgmanage/login.aspx");
                    break;
                case huaguwang.WebControls.WebUserToolBar.enumType.NoPower:
                    page.Exp_PageAlterReplace("您没有权限进行操作", "/hgmanage/login.aspx");
                    break;
                case huaguwang.WebControls.WebUserToolBar.enumType.NoUser:
                    page.Exp_PageAlterReplace("用户资料失效", "/hgmanage/login.aspx");
                    break;
                default:
                    break;
            }

        }
        /// <summary>
        /// 存储用户资料的session名称
        /// </summary>
        public static readonly string StringSessionUserName = "Session_UserItems";
        /// <summary>
        /// 获取当前用户资料
        /// </summary>
        /// <param name="page">页面page</param>
        /// <returns>用户实体,最好是i_user接口资料</returns>
        public static M_T_AdminUser_Exp Exp_GetUser(this System.Web.UI.Page page)
        {
            var item = page.Session[StringSessionUserName];
            try
            {
                return item.Exp_To<M_T_AdminUser_Exp>();
            }
            catch
            {
                return null;
            }
        }
        private static void Exp_SetUser(this System.Web.UI.Page page, M_T_AdminUser bln)
        {
            page.Session.Add(StringSessionUserName, bln);

        }

        /// <summary>
        /// 登录操作,并保存用户信息
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="username">用户名</param>
        /// <param name="pwd">用户密码</param>
        /// <returns>操作是否成功</returns>
        public static bool Exp_Login(this System.Web.UI.Page page, string username, string pwd)
        {
            var TempMoel = new M_T_AdminUser_Exp()
            {
                Flag = true,
                id = 1,
                IsAdmin = true,
                LoginName = "测试",
                PassWord = "测试",
                PowerGroupID = 1,
                PowerGroupName = "测试",
                RealName = "测试"
            };
            page.Exp_SetUser(TempMoel);
            return true;
            //var _dal = new D_T_AdminUser_Exp();
            //var bln = _dal.GetModel(username, pwd);
            //if(bln != null)
            //{
            //    page.SetUser(bln);
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }

        /// <summary>
        /// 登录操作并保存用户信息
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <param name="checkCode">验证码</param>
        /// <param name="errorMsg">错误提示信息</param>
        /// <returns>登录是否成功</returns>
        public static bool Exp_Login(this System.Web.UI.Page page, string userName, string passWord, string checkCode, out string errorMsg)
        {
            var m_dal = new D_T_AdminUser_Exp();
            if(page.Session["CheckCode"] != null && page.Session["CheckCode"].Exp_Trim().Equals(checkCode.Exp_Trim()))
            {
                errorMsg = "验证码不正确";
                return false;
            }
            var vQuery = m_dal.GetModel(userName, passWord);
            if(vQuery != null)
            {
                page.Exp_SetUser(vQuery);
                errorMsg = "";
                return true;
            }
            else
            {
                errorMsg = "登录失败";
                return false;
            }
        }
        /// <summary>
        /// 获得相关的按钮资料
        /// </summary>
        /// <param name="strURL"></param>
        /// <param name="UID"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public static List<HGInterface.I_Button> Exp_GetButtonList(string strURL, int UID, bool isAdmin)
        {
            var _Dal = new D_T_AdminUser_Exp();
            var list = _Dal.GetButton(strURL, UID, isAdmin);
            if(list == null || list._ButtonList == null || list._ButtonList.Count <= 0)
            {
                return new List<HGInterface.I_Button>();
            }
            var ButLIst = from t1 in list._ButtonList
                          select (HGInterface.I_Button)t1;

            return ButLIst.ToList();
        }
    }
}
